Repository navigation
feat: Settings > Diagnostics shows which thread is using the CPU - #1038
Merged
Merged
Conversation
Nothing in the app said which thread's agents were using the machine. The Diagnostics page listed the server's child processes, but none were tied to a thread. A new "CPU by thread" section at the top of the page has one row per thread, one for Threadlines itself, and one for a provider whose single process serves all its threads (OpenCode). Rows are sorted by the average over the last minute, a thread's name opens it, and a row expands to its busiest processes. Ownership comes from what the server already tracks: each driver's agent process ledger (now listed through ProviderAdapterShape.listProcesses and ProviderService.listAgentProcesses) and each terminal's shell. A process belongs to the nearest tracked process above it, or to the process group of an agent that leads one. CPU is what each process used since the read before, from `ps time`. `ps pcpu` is an average over a process's whole life on Linux, so the POSIX process query gained a `time=` column. It adds no timer: the existing on-demand sampler hands each read to the new view, and the page asks only while the section is on screen and the window is in use.
|
The latest updates on your projects. Learn more about Vercel for GitHub. 1 Skipped Deployment
|
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Nothing in the app said which thread's agents were using the machine. Settings > Diagnostics listed the server's child processes with their CPU, but none were tied to a thread. On one Mac, agents ran a browser test suite about 600 times in a day at around 10 cores each, the battery drained at 50 to 60% an hour, and finding the cause took reading session logs.
Fix
A "CPU by thread" section at the top of Settings > Diagnostics.
Whose a process is. Only from what the server already tracks, never from command lines or folders:
owners(), drivers pass it up throughProviderAdapterShape.listProcesses, andProviderService.listAgentProcessesgathers them. One driver that can't answer leaves out only its own processes.TerminalManager.listShells).A process belongs to the nearest tracked process above it. An agent that leads its own process group also keeps the group's processes that were handed to pid 1. Processes outside the server's tree that nobody tracks are left out.
CPU figures. What each process used since the read before, from
ps time.ps pcpuis a recent average on macOS but an average over the process's whole life on Linux, where an agent that idled for an hour and then filled a core would read 2%. The POSIX process query gained atime=column for this. Windows already derives its figure from a running total.No new background cost. The existing sampler runs only while someone asks and stops 2 minutes after the last request. It now hands each read to observers, and the new view is one. The page asks every 5 seconds only while the section is on screen and the window has been used in the last 5 minutes.
Guide. A new place (
diagnostics.cpuByThread) and topic, so agents can answer "which thread is using my CPU".Tests. Grouping: two agents in one thread, nested tracked agents, a child handed to pid 1, a tracked agent that left the server's tree, a reused group id, a shared provider, Windows rows without a group, and the app's own processes. Interval CPU: a long-lived process that turns busy, a reused pid, a gap, and rows without a total. A browser test checks the rows, the order by average, and that new instant values don't reorder rows. It fails when the sort uses the instant value. The full unit suite passes, with fmt, lint and typecheck. The browser suite passes except two tests in
steppedLoops.browser.tsx, which this change doesn't touch; they failed at a load average of 23 and pass when the file runs alone.Real run. On a throwaway server with a stand-in agent, a busy loop the agent started showed under its thread, and the server and its helper under Threadlines. That run was before the interval CPU change. Afterwards the new query, parser and interval figures were checked against this Mac's real process list and a real busy process.
Review. An independent review of the diff found eight problems. Seven are fixed: Linux lifetime averages, polling that never stopped in a focused window, a tracked agent outside the server's tree landing under the wrong thread, one snapshot counted twice in the average, one failing driver emptying every thread's row, a machine total that could exceed the core count shown, and ended processes still listed with CPU. Left as is: an agent's ledger entry outlives it when a child survives, so if those children die and the system reuses the number for a process that leads a group and then exits, the leftover group shows under the old thread. Fixing it means changing when the ledger forgets an entry, which the stop-confirmation path depends on.
Not checked. Linux and Windows. On Linux,
pscounts CPU time in whole seconds, so one process's figure moves in 20% steps between 5-second reads; a group's sum and its one-minute average even that out. On Windows, a process that left its agent's tree isn't found.